/*
 * Copyright (c) 2021.版权所有高金磊
 */

package 领扣算法.A简单题.重新格式化电话号码;

import java.util.LinkedList;

public class Main {
    public String reformatNumber(String number) {
        number = number.replaceAll("-", "").replaceAll(" ", "");
        LinkedList<Character> data = new LinkedList<>();
        for (char c : number.toCharArray()) {
            data.addLast(c);
        }
        StringBuilder stringBuilder = new StringBuilder();
        while (!data.isEmpty()) {
            if (data.size() > 4) {
                stringBuilder.append(data.pollFirst());
                stringBuilder.append(data.pollFirst());
                stringBuilder.append(data.pollFirst());
                if (!data.isEmpty())
                    stringBuilder.append('-');
            } else if (data.size() == 4) {
                stringBuilder.append(data.pollFirst());
                stringBuilder.append(data.pollFirst());
                stringBuilder.append('-');
                stringBuilder.append(data.pollFirst());
                stringBuilder.append(data.pollFirst());
            } else {
                stringBuilder.append(data.pollFirst());
            }

        }

        return stringBuilder.toString();
    }
}
